package com.example.tang.zhayan.ui;

import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ZoomControls;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.example.tang.zhayan.R;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 * Created by yang on 2015/12/17.
 */

public class MapActivity extends AppCompatActivity implements View.OnClickListener {

    private MapView mMapView;
    private BaiduMap mBaiduMap;
    private LocationClient mLocationClient;
    private LocationClientOption options;

    @ViewInject(R.id.back_iv)
    private ImageView iv_back;

    @ViewInject(R.id.tv_find)
    private TextView tv_find;

    @ViewInject(R.id.tv_name)
    private TextView tv_name;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        x.view().inject(this);
        initView();
        Intent intent = getIntent();
        String storeName = intent.getStringExtra("storeName");
        tv_name.setText(storeName + "的店面");
        mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(
                MyLocationConfiguration.LocationMode.FOLLOWING, true, BitmapDescriptorFactory
                .fromResource(R.mipmap.icon_geo)));
        mLocationClient = new LocationClient(getApplicationContext());
        // 配置定位SDK各配置参数
        options = new LocationClientOption();
        initLocationClientOptions();
        // 注册定位监听函数
        if (mBaiduMap != null) {
            mLocationClient.registerLocationListener(new BDLocationListener() {
                @Override
                public void onReceiveLocation(BDLocation arg0) {
                    if (arg0.getLocType() == 63) {

                        Toast.makeText(MapActivity.this, " 网络异常，没有成功向服务器发起请求，请确认当前测试手机网络是否通畅，尝试重新请求定位。", Toast.LENGTH_SHORT).show();
                    }
                    // 启动定位图层
                    mBaiduMap.setMyLocationEnabled(true);
                    // 设置定位的数据
                    mBaiduMap.setMyLocationData(new MyLocationData.Builder().latitude(arg0.getLatitude())
                            .longitude(arg0.getLongitude()).build());
                    mBaiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(new MapStatus.Builder()
                            .target(new LatLng(arg0.getLatitude(), arg0.getLongitude())).zoom(18).build()));
                }
            });
        }
        mLocationClient.start();

    }

    private void initView() {
        mMapView = (MapView) findViewById(R.id.bmapView);
        mBaiduMap = mMapView.getMap();
//        隐藏地图上缩放空间和百度地图logo图标
        View child = mMapView.getChildAt(1);
        // 隐藏百度logo和缩放控件ZoomControl
        if (child instanceof ImageView || child instanceof ZoomControls) {
            child.setVisibility(View.INVISIBLE);
        }

//    隐藏地图上比例尺

        mMapView.removeViewAt(2);
        tv_find.setOnClickListener(this);
        iv_back.setOnClickListener(this);
    }

    // 设置定位SDK参数
    private void initLocationClientOptions() {
        options.setCoorType("bd09ll");
        options.setOpenGps(true);
        options.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        options.setScanSpan(0);
        options.setIsNeedAddress(true);
        mLocationClient.setLocOption(options);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        if(mMapView != null){
            mMapView.onDestroy();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mMapView.onPause();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.tv_find:
                break;
        }
    }


}
